package com.gx.smart.building.ui.common.util;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.gx.smart.building.R;
import com.mylhyl.circledialog.CircleDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

/**
 * @author: xiaosy
 * 时间: 2021/8/9
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class SpannableUtil {
    private Context context;

    public SpannableUtil(Context context) {
        this.context = context;
    }

    //登录页面
    public void initSpanPhone(TextView textView) {
        String content = textView.getText().toString();
        SpannableString spStr = new SpannableString(content);
        if (content.indexOf("(") != -1) {
            int start = content.indexOf("(");
            int end = content.indexOf(")");
            TextViewClickableSpan clickableSpan11 = new TextViewClickableSpan(v -> {
                new CircleDialog.Builder()
                        .setTitle("拨打电话")
                        .setText("您确定要拨打电话吗？")
                        .setPositive("确定", v1 -> grantedCallPhonePermission())
                        .setNegative("取消", null)
                        .show(((AppCompatActivity) context).getSupportFragmentManager());
            });
            setColorClickable(spStr, clickableSpan11, start, end);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
        textView.setText(spStr);
    }

    private void grantedCallPhonePermission() {
        // Storage permission are allowed.
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            Intent intent = IntentUtils.getCallIntent("0571-55555555");
            ActivityUtils.startActivity(intent);
            return;
        }
        AndPermission.with(context)
                .runtime()
                .permission(Permission.CALL_PHONE)
                .onGranted(permissions -> {
                    permissions.contains(Permission.CALL_PHONE);
                    // Storage permission are allowed.
                    grantedCallPhonePermission();
                })
                .onDenied(permissions -> {
                    // Storage permission are not allowed.
                    new CircleDialog.Builder()
                            .setTitle("获取权限")
                            .setText("请允许获取对应的权限，否则不能运行？")
                            .setPositive("确定", null)
                            .show(((AppCompatActivity) context).getSupportFragmentManager());
                }).start();
    }

    public void setColorClickable(SpannableString spStr,
                                  TextViewClickableSpan clickableSpan, int start, int end) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.font_color_style_two));
        spStr.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        spStr.setSpan(clickableSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }

    public void setFont(SpannableString spStr, int start, int end) {
        StyleSpan styleSpan = new StyleSpan(android.graphics.Typeface.BOLD);
        spStr.setSpan(styleSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }


}
